% The original SF is (cycle/pixel), need to convert it as (cycle/degree)
% Formula: =(SF(CPP)*3.1416*24*280)/180 CPD
% CPP = cycle/pixel, CPD = cycle/degree
% VD = 280

clear all
close all
clc

colors = [211 57 33; 2 158 154]/255;
scale_factor_far = 0.0085;

%% Far distance
load('training_data_all.mat');
load('subject_specification.mat');

for ss = 1:8
    for dd = 1:20
        mean_sf_day = [];
        load_data_all = [];
        for bb = 1:10
            load_data = far.data_all{1,ss}{dd,bb};
            period_list_STR = (load_data(:,7));
            temp = cal_reversal_SF(period_list_STR);
            load_data_all = [load_data_all;load_data]; 
            mean_sf_day = [ mean_sf_day; temp];
        end
           
        mean_sf = mean(mean_sf_day);
        median_rt = median( load_data_all(:,8));
        
        far_sum_t.sf(ss,dd) = mean_sf;
        far_sum_t.rt(ss,dd) = median_rt;
    end
end

% axis_sf = cal_axis(far_sum_t.sf);
% axis_rt = cal_axis(far_sum_t.rt);
figure('color',[1 1 1])
for ss = 1:8
    subplot(4,2,ss)
    %[ax, ~, ~] = plotyy(1:20,(far_sum_t.sf(ss,:)*3.1416*24*280)/180,1:20,far_sum_t.rt(ss,:));
    [ax, ~, ~] = plotyy(1:20,(far_sum_t.sf(ss,:))/scale_factor_far,1:20,far_sum_t.rt(ss,:));
    ylabel(ax(1),'Spatial Frequency (cycle / deg)');
    ylabel(ax(2),'Reaction Time (s)');
    xlabel(ax(1),'Days')
    hold(ax(1),'on');
    hold(ax(2),'on');

    title(['Subject' num2str(ss)]);
end
sgtitle('Exp1 Far')


%% Near distance

for ss = 1:8
    for dd = 1:20
        temp = [];
        temp2 = [];
        for bb = 1:10
            temp2 = [temp2; near.data_all{1,ss}{dd,bb}];
            temp = [temp; near.distance{1,ss}{dd,bb}];
        end
        
        mean_cd = mean(temp);
        median_rt = median(temp2(:,8));
        
        
        near_sum_t.cd(ss,dd) = mean_cd;
        near_sum_t.rt(ss,dd) = median_rt;
    end
end

y = near_sum_t.cd(ss,:);
z = near_sum_t.rt(ss,:);
figure('color',[1 1 1])
for ss = 1:8
    subplot(4,2,ss)
    [ax, ~, ~] = plotyy(1:20,near_sum_t.cd(ss,:),1:20,near_sum_t.rt(ss,:));
    ylabel(ax(1),'Distance (cm)');
    ylabel(ax(2),'Reaction Time (s)');
    xlabel(ax(1),'Days')
    hold(ax(1),'on');
    hold(ax(2),'on');

    title(['Subject' num2str(ss)]);
end
sgtitle('Exp1 Near')


%% Far distance

load('control_training_all.mat');
load('subject_specification.mat');

for ss = 1:8
        for dd = 1:20
        %temp = [];
        mean_sf_day = [];
        load_data_all = [];
        blocks = 5;
        if (ss == 1 && dd == 1) || (ss == 1 && dd == 3)
            blocks = 8;
        end
        for bb = 1:blocks
            load_data = far.data_all{1,ss}{dd,bb};
            
            if isempty(load_data)
                period_list_STR = [];
                temp = [];
                
            else
            
                period_list_STR = (load_data(:,7));
                temp = cal_reversal_SF(period_list_STR);
                
            end
            
            mean_sf_day = [mean_sf_day; temp];
            load_data_all = [load_data_all;load_data]; 
        end
        

        if isempty(mean_sf_day)
            mean_sf = 0;
            median_rt = 0;
        else    
            mean_sf = mean(mean_sf_day);
            median_rt = median( load_data_all(:,8));
        end
        far_sum_c.sf(ss,dd) = mean_sf;
        far_sum_c.rt(ss,dd) = median_rt;
    end
end

far_sum_c.rt(1,2) = far_sum_c.rt(1,1);
far_sum_c.sf(1,2) = far_sum_c.sf(1,1);

% axis_sf = cal_axis(far_sum_t.sf);
% axis_rt = cal_axis(far_sum_t.rt);
y = far_sum_c.sf(ss,:);
z = far_sum_c.rt(ss,:);
figure('color',[1 1 1])
for ss = 1:8
    subplot(4,2,ss)
    %[ax, ~, ~] = plotyy(1:20,far_sum_c.sf(ss,:),1:20,far_sum_c.rt(ss,:));
%     [ax, ~, ~] = plotyy(1:20,(far_sum_c.sf(ss,:)*3.1416*24*280)/180,1:20,far_sum_c.rt(ss,:));
    [ax, ~, ~] = plotyy(1:20,(far_sum_c.sf(ss,:))/scale_factor_far,1:20,far_sum_c.rt(ss,:));
    ylabel(ax(1),'Spatial Frequency (cycle / deg)');
    ylabel(ax(2),'Reaction Time (s)');
    xlabel(ax(1),'Days')
    hold(ax(1),'on');
    hold(ax(2),'on');

    title(['Subject' num2str(ss)]);
end
sgtitle('Exp2 Far')


%% Near distance

for ss = 1:8
    for dd = 1:20
        temp = [];
        temp2 = [];
        blocks = 5;
        if ss == 1 && dd == 2
            blocks = 8;
        end
        for bb = 1:blocks
            temp2 = [temp2; near.data_all{1,ss}{dd,bb}];
            temp = [temp; near.distance{1,ss}{dd,bb}];
        end
        if isempty(temp)
            mean_cd = 0;
        else
            mean_cd = mean(temp);    
        end
        if isempty(temp2)
            median_rt = 0;
        else
            median_rt = median(temp2(:,8));
        end
        
        near_sum_c.cd(ss,dd) = mean_cd;
        near_sum_c.rt(ss,dd) = median_rt;
    end
end

near_sum_c.cd(1,1) = near_sum_c.cd(1,2);
near_sum_c.cd(1,3) = near_sum_c.cd(1,2);

near_sum_c.rt(1,1) = near_sum_c.rt(1,2);
near_sum_c.rt(1,3) = near_sum_c.rt(1,2);

y = near_sum_c.cd(ss,:);
z = near_sum_c.rt(ss,:);
figure('color',[1 1 1])
for ss = 1:8
    subplot(4,2,ss)
    [ax, ~, ~] = plotyy(1:20,near_sum_c.cd(ss,:),1:20,near_sum_c.rt(ss,:));
    ylabel(ax(1),'Distance (cm)');
    ylabel(ax(2),'Reaction Time (s)');
    xlabel(ax(1),'Days')
    hold(ax(1),'on');
    hold(ax(2),'on');

    title(['Subject' num2str(ss)]);
end
sgtitle('Exp2 Near')


%%
figure('color',[1 1 1])
for tttt = 1:4
    switch tttt
        case 1; temp1 = (far_sum_t.sf)/scale_factor_far; temp2 = far_sum_t.rt; titletex = 'Exp1 Far Distance'; xlabeltex = 'Spatial Frequency (cycle/deg)';
        case 2; temp1 = (far_sum_c.sf)/scale_factor_far; temp2 = far_sum_c.rt; titletex = 'Exp2 Far Distance'; xlabeltex = 'Spatial Frequency (cycle/deg)';
        case 3; temp1 = near_sum_t.cd; temp2 = near_sum_t.rt; titletex = 'Exp1Near Distance'; xlabeltex = 'Distance to Eye (cm)';
        case 4; temp1 = near_sum_c.cd; temp2 = near_sum_c.rt; titletex = 'Exp2 Far Distance'; xlabeltex = 'Distance to Eye (cm)';
   
    end
    subplot(2,2,tttt)
    plot(temp1,temp2,'ko','MarkerFaceColor',[1 1 1]); hold on;
    for sss = 1:8
        fitted = fitlm(temp1(sss,:),temp2(sss,:));
        fitted_line = fitted.Coefficients.Estimate(1)+temp1(sss,:)*fitted.Coefficients.Estimate(2);
        plot(temp1(sss,:),fitted_line,'linewidth',1); hold on;
        xlabel(xlabeltex); ylabel('Reaction Time (s)');
        title(titletex);
        slope_all(tttt,sss) = fitted.Coefficients.Estimate(2);
    end
end
    
%% Power function to check learning effect
f = @(b,x) b(1).*x.^b(2)+b(3);
SSE = @(b,f,x,y) sum((y-f(b,x)).^2);

rt_change_f_t = [];
rt_change_n_t = [];
rt_change_f_c = [];
rt_change_n_c = [];
for sub = 1:8
    
    %x1 = 1:20; y = far_sum_t.sf(sub,:);
%     x1 = 1:20; y = (far_sum_t.sf(sub,:)*3.1416*24*280)/180;
    x1 = 1:20; y = (far_sum_t.sf(sub,:))/scale_factor_far;
    B1 = fminsearch(@(b) SSE(b,f,x1,y), [1, -1, 0]);
    train_far_cd(sub,:) = B1;
    x2 = 1:20; y = near_sum_t.cd(sub,:);
    B2 = fminsearch(@(b) SSE(b,f,x2,y), [1, -1, 0]);
    train_near_sf(sub,:) = B2;
    
    x3 = 1:20; y = (far_sum_c.sf(sub,:))/scale_factor_far;
    B3 = fminsearch(@(b) SSE(b,f,x1,y), [1, -1, 0]);
    con_far_cd(sub,:) = B3;
    x4 = 1:20; y = near_sum_c.cd(sub,:);
    B4 = fminsearch(@(b) SSE(b,f,x2,y), [1, -1, 0]);
    con_near_sf(sub,:) = B4;

    x5 = 1:20; y = far_sum_t.rt(sub,:);
    B5 = fminsearch(@(b) SSE(b,f,x5,y), [1, -1, 0]);
    train_far_rt(sub,:) = B5;
    x6 = 1:20; y = near_sum_t.rt(sub,:);
    B6 = fminsearch(@(b) SSE(b,f,x6,y), [1, -1, 0]);
    train_near_rt(sub,:) = B6;
    
    x7 = 1:20; y = far_sum_c.rt(sub,:);
    B7 = fminsearch(@(b) SSE(b,f,x7,y), [1, -1, 0]);
    con_far_rt(sub,:) = B7;
    x8 = 1:20; y = near_sum_c.rt(sub,:);
    B8 = fminsearch(@(b) SSE(b,f,x8,y), [1, -1, 0]);
    con_near_rt(sub,:) = B8;
    
    estimation_train_distance(:,sub) = (B2(1).*x1.^B2(2)+B2(3));
    estimation_train_rt_near(:,sub) = (B6(1).*x1.^B6(2)+B6(3));
    estimation_train_sf(:,sub) = (B1(1).*x1.^B1(2)+B1(3));
    estimation_train_rt_far(:,sub) = (B5(1).*x1.^B5(2)+B5(3));
    
    estimation_con_distance(:,sub) = (B4(1).*x1.^B4(2)+B4(3));
    estimation_control_rt_near(:,sub) = (B8(1).*x1.^B8(2)+B8(3));
    estimation_con_sf(:,sub) = (B3(1).*x1.^B3(2)+B3(3));
    estimation_control_rt_far(:,sub) = (B7(1).*x1.^B7(2)+B7(3));
    
    
    figure(55)
    subplot(2,2,1)
%     plot(1:20,(far_sum_t.sf(sub,:)*3.1416*24*280)/180,'o-',x1,B1(1).*x1.^B1(2)+B1(3),'r-'); title('Exp1'); hold on;
    plot(1:20,(far_sum_t.sf(sub,:))/scale_factor_far,'o-',x1,B1(1).*x1.^B1(2)+B1(3),'r-'); title('Exp1'); hold on;
    xlabel('Days'); ylabel('Spatial Frequency (cycle / deg)');
    subplot(2,2,2)
    plot(1:20,far_sum_t.rt(sub,:),'o-',x1,B5(1).*x1.^B5(2)+B5(3),'r-'); title('Exp1'); hold on;
    xlabel('Days'); ylabel('Response Time (s)');
    subplot(2,2,3)
%     plot(1:20,(far_sum_c.sf(sub,:)*3.1416*24*280)/180,'o-',x3,B3(1).*x3.^B3(2)+B3(3),'r-'); title('Exp2'); hold on;
    plot(1:20,(far_sum_c.sf(sub,:))/scale_factor_far,'o-',x3,B3(1).*x3.^B3(2)+B3(3),'r-'); title('Exp2'); hold on;
    xlabel('Days'); ylabel('Spatial Frequency (cycle / deg)');
    subplot(2,2,4)
    plot(1:20,far_sum_c.rt(sub,:),'o-',x3,B7(1).*x3.^B7(2)+B7(3),'r-'); title('Exp2'); hold on;
    xlabel('Days'); ylabel('Response Time (s)');
    sgtitle('Far Distance')
    
    figure(56)
    subplot(2,2,1)
    plot(1:20,near_sum_t.cd(sub,:),'o-',x2,B2(1).*x2.^B2(2)+B2(3),'r-'); title('Exp1'); hold on;
    xlabel('Days'); ylabel('Distance to Eye (cm)');
    subplot(2,2,2)
    plot(1:20,near_sum_t.rt(sub,:),'o-',x3,B6(1).*x3.^B6(2)+B6(3),'r-'); title('Exp1'); hold on;
    xlabel('Days'); ylabel('Response Time (s)');
    subplot(2,2,3)
    plot(1:20,near_sum_c.cd(sub,:),'o-',x2,B4(1).*x2.^B4(2)+B4(3),'r-'); title('Exp2'); hold on;
    xlabel('Days'); ylabel('Distance to Eye (cm)');
    subplot(2,2,4)
    plot(1:20,near_sum_c.rt(sub,:),'o-',x3,B8(1).*x3.^B8(2)+B8(3),'r-'); title('Exp2'); hold on;
    xlabel('Days'); ylabel('Response Time (s)');
    sgtitle('Near Distance')

    rt_ini_f_t(sub,1) = B5(1)*1.^B5(2)+B5(3);
    rt_ini_f_c(sub,1) = B7(1)*1.^B7(2)+B7(3);

    rt_ini_n_t(sub,1) = B6(1)*1.^B6(2)+B6(3);
    rt_ini_n_c(sub,1) = B8(1)*1.^B8(2)+B8(3);
    
    rt_end_f_t(sub,1) = B5(1)*20.^B5(2)+B5(3);
    rt_end_f_c(sub,1) = B7(1)*20.^B7(2)+B7(3);

    rt_end_n_t(sub,1) = B6(1)*20.^B6(2)+B6(3);
    rt_end_n_c(sub,1) = B8(1)*20.^B8(2)+B8(3);

    rt_change_f_t(sub,1) = B5(1)*1.^B5(2)+B5(3) - (B5(1)*20.^B5(2)+B5(3));
    rt_change_f_c(sub,1) = B7(1)*1.^B7(2)+B7(3) - (B7(1)*20.^B7(2)+B7(3));
    
    rt_change_n_t(sub,1) = B6(1)*1.^B6(2)+B6(3) - (B6(1)*20.^B6(2)+B6(3));
    rt_change_n_c(sub,1) = B8(1)*1.^B8(2)+B8(3) - (B8(1)*20.^B8(2)+B8(3)); % Positive means decrement
    
    rt_m_f_t(sub,1) = mean((B5(1)*(1:20)).^B5(2) + B5(3));
    rt_m_f_c(sub,1) = mean((B7(1)*(1:20)).^B7(2) + B7(3));
    
    rt_m_n_t(sub,1) = mean((B6(1)*(1:20)).^B6(2) + B6(3));
    rt_m_n_c(sub,1) = mean((B8(1)*(1:20)).^B8(2) + B8(3));
    
    
    sf_ini_f_t(sub,1) = B1(1)*1.^B1(2)+B1(3);
    sf_ini_f_c(sub,1) = B3(1)*1.^B3(2)+B3(3);

    cd_ini_n_t(sub,1) = B2(1)*1.^B2(2)+B2(3);
    cd_ini_n_c(sub,1) = B4(1)*1.^B4(2)+B4(3);
    
    sf_end_f_t(sub,1) = B1(1)*20.^B1(2)+B1(3);
    sf_end_f_c(sub,1) = B3(1)*20.^B3(2)+B3(3);

    cd_end_n_t(sub,1) = B2(1)*20.^B2(2)+B2(3);
    cd_end_n_c(sub,1) = B4(1)*20.^B4(2)+B4(3);

    sf_change_f_t(sub,1) = B1(1)*20.^B1(2)+B1(3) - (B1(1)*1.^B1(2)+B1(3));
    sf_change_f_c(sub,1) = B3(1)*20.^B3(2)+B3(3) - (B3(1)*1.^B3(2)+B3(3));
    
    cd_change_n_t(sub,1) = B2(1)*1.^B2(2)+B2(3) - (B2(1)*20.^B2(2)+B2(3));
    cd_change_n_c(sub,1) = B4(1)*1.^B4(2)+B4(3) - (B4(1)*20.^B4(2)+B4(3)); % Positive means decrement
    
end
% 


days = 1:20; training_distance_observation = mean(near_sum_t.cd);
tdist = fminsearch(@(b) SSE(b,f,days,training_distance_observation), [1, -1, 0])';

%days = 1:20; training_spatial_frequency_observation = mean(far_sum_t.sf);
%days = 1:20; training_spatial_frequency_observation = mean((far_sum_t.sf*3.1416*24*280)/180);
days = 1:20; training_spatial_frequency_observation = mean((far_sum_t.sf)/scale_factor_far);
tsf = fminsearch(@(b) SSE(b,f,days,training_spatial_frequency_observation), [1, -1, 0])';

days = 1:20; control_distance_observation = mean(near_sum_c.cd);
cdist = fminsearch(@(b) SSE(b,f,days,control_distance_observation), [1, -1, 0])';

% days = 1:20; control_spatial_frequency_observation = mean(far_sum_c.sf);
% days = 1:20; control_spatial_frequency_observation = mean((far_sum_c.sf*3.1416*24*280)/180);
days = 1:20; control_spatial_frequency_observation = mean((far_sum_c.sf)/scale_factor_far);
csf = fminsearch(@(b) SSE(b,f,days,control_spatial_frequency_observation), [1, -1, 0])';

%% Mean power function fitting for near distance
colors2 = [2 158 154; 255 189 7; 211 57 33]/255;

% Side-by-side figures
figure('color',[1 1 1])
subplot(1,2,1)
ss1 = shadedErrorBar(1:20,mean(near_sum_t.cd),std(near_sum_t.cd)/sqrt(8),colors2(1,:));
set(ss1.edge, 'linestyle','none'); hold on
ss1 = shadedErrorBar(1:20,mean(near_sum_c.cd),std(near_sum_c.cd)/sqrt(8),colors2(2,:));
set(ss1.edge, 'linestyle','none'); hold on
p1 = plot(0.5:0.5:20,tdist(1).*(0.5:0.5:20).^tdist(2)+tdist(3),'color',colors2(1,:),'linewidth',2); hold on
p2 = plot(0.5:0.5:20,cdist(1).*(0.5:0.5:20).^cdist(2)+cdist(3),'color',colors2(2,:),'linewidth',2);

set(gca,'xlim',[0 20],'ylim',[10 45],'FontName', 'Arial','FontSize', 10, 'linewidth',1)
ylabel('Screen distance-to-eye (cm)', 'FontName', 'Arial','FontSize', 10)
xlabel('Training session (day)', 'FontName', 'Arial','FontSize', 10)
title('Near distance', 'FontName', 'Arial','FontSize', 11);

subplot(1,2,2)
ss1 = shadedErrorBar(1:20,mean(far_sum_t.sf/scale_factor_far),std(far_sum_t.sf/scale_factor_far)/sqrt(8),colors2(1,:));
set(ss1.edge, 'linestyle','none'); hold on
ss1 = shadedErrorBar(1:20,mean(far_sum_c.sf/scale_factor_far),std(far_sum_c.sf/scale_factor_far)/sqrt(8),colors2(2,:));
set(ss1.edge, 'linestyle','none'); hold on
p1 = plot(0.5:0.5:20,tsf(1).*(0.5:0.5:20).^tsf(2)+tsf(3),'color',colors2(1,:),'linewidth',2); hold on
p2 = plot(0.5:0.5:20,csf(1).*(0.5:0.5:20).^csf(2)+csf(3),'color',colors2(2,:),'linewidth',2);
legend([p1, p2],'Alternating','Fixed','location','northwest')
legend box off
%set(gca,'xlim',[0 20],'ylim',[0.015 0.1],'FontSize', 10, 'linewidth',1)
% % set(gca,'xlim',[0 20],'ylim',[0 12],'FontSize', 10, 'linewidth',1)
% % ylabel('Spatial frequency (cycle/deg)','FontSize', 10)
% % xlabel('Training session (day)', 'FontSize', 10)
set(gca,'xlim',[0 20],'ylim',[0 12],'FontName', 'Arial','FontSize', 10, 'linewidth',1)
ylabel('Spatial frequency (cycle/deg)','FontName', 'Arial','FontSize', 10)
xlabel('Training session (day)','FontName', 'Arial', 'FontSize', 10)
title('Far distance');

set(gcf,'units','centimeters','position',[0 0 19 9])

% Up_down figures
figure('color',[1 1 1])
subplot(2,1,1)
ss1 = shadedErrorBar(1:20,mean(near_sum_t.cd),std(near_sum_t.cd)/sqrt(8),colors2(1,:));
set(ss1.edge, 'linestyle','none'); hold on
ss1 = shadedErrorBar(1:20,mean(near_sum_c.cd),std(near_sum_c.cd)/sqrt(8),colors2(2,:));
set(ss1.edge, 'linestyle','none'); hold on
p1 = plot(0.5:0.5:20,tdist(1).*(0.5:0.5:20).^tdist(2)+tdist(3),'color',colors2(1,:),'linewidth',2); hold on
p2 = plot(0.5:0.5:20,cdist(1).*(0.5:0.5:20).^cdist(2)+cdist(3),'color',colors2(2,:),'linewidth',2);


set(gca,'xlim',[0 20],'ylim',[10 45],'FontName', 'Arial','FontSize', 10, 'linewidth',1)
legend([p1, p2],'Alternating','Fixed','location','northeast')
legend box off
ylabel('Screen distance-to-eye (cm)','FontName', 'Arial','FontSize', 10)
xlabel('Training session (day)','FontName', 'Arial', 'FontSize', 10)
title('Near distance', 'FontName', 'Arial','FontSize', 11);

subplot(2,1,2)
ss1 = shadedErrorBar(1:20,mean((far_sum_t.sf)/scale_factor_far),std((far_sum_t.sf)/scale_factor_far)/sqrt(8),colors2(1,:));
set(ss1.edge, 'linestyle','none'); hold on
ss1 = shadedErrorBar(1:20,mean((far_sum_c.sf)/scale_factor_far),std((far_sum_c.sf)/scale_factor_far)/sqrt(8),colors2(2,:));
set(ss1.edge, 'linestyle','none'); hold on
p1 = plot(0.5:0.5:20,tsf(1).*(0.5:0.5:20).^tsf(2)+tsf(3),'color',colors2(1,:),'linewidth',2); hold on
p2 = plot(0.5:0.5:20,csf(1).*(0.5:0.5:20).^csf(2)+csf(3),'color',colors2(2,:),'linewidth',2);
set(gca,'xlim',[0 20],'ylim',[0 12],'FontName', 'Arial','FontSize', 10, 'linewidth',1)
ylabel({'Spatial frequency'; '(cycle/deg)'},'FontName', 'Arial','FontSize', 10)
xlabel('Training session (day)','FontName', 'Arial', 'FontSize', 10)
title('Far distance');

set(gcf,'units','centimeters','position',[0 0 19 20])


% Calculate the learning index for SF and distance
diff_training_distance = estimation_train_distance(1,:) - estimation_train_distance(end,:);
diff_training_rt_near =  estimation_train_rt_near(1,:) - estimation_train_rt_near(end,:);
diff_training_sf = estimation_train_sf(end,:) - estimation_train_sf(1,:);
% diff_training_sf = estimation_train_sf(1,:) - estimation_train_sf(end,:);
diff_training_rt_far = estimation_train_rt_far(1,:) - estimation_train_rt_far(end,:);

diff_con_distance = estimation_con_distance(1,:) - estimation_con_distance(end,:);
diff_control_rt_near =  estimation_control_rt_near(1,:) - estimation_control_rt_near(end,:);
diff_con_sf = estimation_con_sf(end,:) - estimation_con_sf(1,:);
% diff_con_sf = estimation_con_sf(1,:) - estimation_con_sf(end,:);
diff_control_rt_far = estimation_control_rt_far(1,:) - estimation_control_rt_far(end,:);


%% Draw the Learning Index

%for near distance
xaxisF = [1 2];
subplot(1,2,1)
mmm = [mean(diff_training_distance) mean(diff_con_distance)];
dataF = [diff_training_distance' diff_con_distance'];
colors = [2 158 154; 255 189 7]/255;
colors2 = colors*1.2;
colors2(colors2>1) = 1;
colorsF = repelem(colors,2,1);
% colorsF2 = repelem(colors2,2,1);
colorsF2 = repelem(colors2,1,1);

for bb = 1:2
    b = bar(bb,mmm(bb),'linewidth',1); hold on
    b.FaceColor = 'flat';
%     b.CData(1,:) = colors2(bb,:);
    b.CData(1,:) = colors(bb,:);
    b.BarWidth = 0.6;
    hold on
    % plot(bb,dataF(:,bb),'o', 'MarkerEdgeColor' ,'none', 'MarkerFaceColor' ,colorsF2(bb,:),'MarkerSize',7); 
    plot(bb,dataF(:,bb),'o', 'MarkerEdgeColor' ,[0.7 0.7 0.7], 'MarkerFaceColor' ,colorsF2(bb,:),'MarkerSize',7); 
    hold on
    bars{bb,1} = b;
end

errorbar([1,2],[mean(diff_training_distance) mean(diff_con_distance)], [std(diff_training_distance) std(diff_con_distance)]/sqrt(8),'k','LineStyle','none','linewidth' , 1,'Marker','o','MarkerFaceColor',[1 1 1],'CapSize',0,'MarkerSize',7);

set(gca,'ylim',[0 40],'xtick',[],'FontName', 'Arial','FontSize', 10,'LineWidth', 1)
box off
ylabel('Decrement of distance-to-eye (cm)','FontName', 'Arial','FontSize', 10)
title({'Near screen'}, 'FontName', 'Arial','FontSize', 11)

% for far distance
xaxisF = [1 2];
subplot(1,2,2)
mmm = [mean(diff_training_sf) mean(diff_con_sf)];
dataF = [diff_training_sf' diff_con_sf'];
% % colors = [211 57 33; 2 158 154]/255;


for bb = 1:2
    b = bar(bb,mmm(bb),'linewidth',1); hold on
    b.FaceColor = 'flat';
%     b.CData(1,:) = colors2(bb,:);
    b.CData(1,:) = colors(bb,:);
    b.BarWidth = 0.6;
    hold on
    bbbb(bb) = b;
%     plot(bb,dataF(:,bb),'o', 'MarkerEdgeColor' ,'none', 'MarkerFaceColor' ,colorsF2(bb,:),'MarkerSize',7); 
    plot(bb,dataF(:,bb),'o', 'MarkerEdgeColor' ,[0.7 0.7 0.7], 'MarkerFaceColor' ,colorsF2(bb,:),'MarkerSize',7); 
    
    hold on
    bars{bb,1} = b;
end

errorbar([1,2],[mean(diff_training_sf) mean(diff_con_sf)], [std(diff_training_sf) std(diff_con_sf)]/sqrt(8),'k','LineStyle','none','linewidth' , 1,'Marker','o','MarkerFaceColor',[1 1 1],'CapSize',0,'MarkerSize',7);

set(gca,'ylim',[0 12],'xtick',[],'FontName', 'Arial','FontSize', 10,'LineWidth', 1)
box off
ylabel('Increment of SF (cpd)','FontName', 'Arial','FontSize', 10)
title({'Far screen'}, 'FontName', 'Arial','FontSize', 11)
 
legend([bbbb(1) bbbb(2)],'Alternating','Fixed','location','northwest')
legend box off

set(gcf,'units','centimeters','position',[0 0 19 9])

%%




% draw the percentage improvement of the 
% Calculate the % improvement learning index for SF and distance
diff_training_distance_percent = (diff_training_distance(1,:)./estimation_train_distance(1,:))*100;
diff_training_sf_percent = (diff_training_sf./estimation_train_sf(1,:))*100;

diff_con_distance_percent = (diff_con_distance./estimation_con_distance(1,:))*100;
diff_con_sf_percent = (diff_con_sf./estimation_con_sf(1,:))*100;


% for near distance
colors2 = [2 158 154; 255 189 7; 211 57 33]/255;
figure('color',[1 1 1])
subplot(1,2,1)
b = bar([mean(diff_training_distance_percent) mean(diff_con_distance_percent)],'linewidth',1); hold on;
b.FaceColor = 'flat';
b.CData(1,:) = colors2(1,:);
b.CData(2,:) = colors2(2,:);
b.BarWidth = 0.6;
errorbar([1 2],[mean(diff_training_distance_percent) mean(diff_con_distance_percent)], [std(diff_training_distance_percent) std(diff_con_distance_percent)]/sqrt(8),'k','LineStyle','none','linewidth' , 1,'Marker','o','MarkerFaceColor',[1 1 1],'CapSize',0,'MarkerSize',7);
%set(gca,'xticklabel',{'Alternating distance training','Fixed distance training'},'FontSize', 12, 'FontWeight', 'bold', 'LineWidth', 1)
set(gca,'xlim',[0.2 2.8],'ylim',[0 70], 'xtick',[],'FontSize', 10,'LineWidth', 1)
ylabel('Improvement of distance-to-eye (%)', 'FontSize', 10)
box off;
title({'Near screen'}, 'FontSize', 11)



% for far distance
subplot(1,2,2)
mmm = [mean(diff_training_sf_percent) mean(diff_con_sf_percent)];
for bb = 1:2
    b = bar(bb,mmm(bb),'linewidth',1); hold on
    b.FaceColor = 'flat';
    b.CData(1,:) = colors2(bb,:);
    b.BarWidth = 0.6;
end
errorbar([1,2],[mean(diff_training_sf_percent) mean(diff_con_sf_percent)], [std(diff_training_sf_percent) std(diff_con_sf_percent)]/sqrt(8),'k','LineStyle','none','linewidth' , 1,'Marker','o','MarkerFaceColor',[1 1 1],'CapSize',0,'MarkerSize',7);
%set(gca,'ylim',[0 0.07],'xtick',[],'FontSize', 10,'LineWidth', 1)
%set(gca,'ylim',[0 0.08],'xtick',[],'FontSize', 10,'LineWidth', 1)
set(gca,'ylim',[0 350],'xtick',[],'FontSize', 10,'LineWidth', 1)
box off;
ylabel('Improvement of SF (%)','FontSize', 10)
title({'Far screen'}, 'FontSize', 11)
legend('Alternating','Fixed','location','northwest')
legend box off;

set(gcf,'units','centimeters','position',[0 0 19 9]);
